<?php

namespace App\Containers\AppSection\User\Actions;

use App\Containers\AppSection\Profile\Tasks\CreateProfileTask;
use App\Containers\AppSection\User\Models\User;
use App\Containers\AppSection\User\Tasks\CreateUserByCredentialsTask;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Requests\Request;
use Illuminate\Support\Facades\Hash;

class RegisterInnerUserAction extends Action
{
    public function run(Request $request): User
    {
        //Создание пользователя
        $user = app(CreateUserByCredentialsTask::class)
            ->run([
                'password' => Hash::make($request->getInputByKey('password')),
                'email' => $request->getInputByKey('email'),
                'fio' => $request->getInputByKey('fio'),
                'status_key' => 'users_ok',
                'email_verified_at' => now(),
                'phone' => $request->getInputByKey('phone'),
                'phone_verified_at' => now(),
                'other_information' => $request->getInputByKey('other_information')
            ], $request->getInputByKey('roles'));

        //Создание профиля
        app(CreateProfileTask::class)
            ->run([
                'user_id' => $user->id,
                'phone' => $request->getInputByKey('phone'),
            ]);

        return $user;
    }
}
